package cn.xfdzx.android.apps.shop.fragment.car;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.ConfirmOrderActivity;
import cn.xfdzx.android.apps.shop.adapter.CarRetailAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.ShopCarListContract;
import cn.xfdzx.android.apps.shop.evenbus.CarInitDataMessage;
import cn.xfdzx.android.apps.shop.evenbus.CarVideoInitDataMessage;
import cn.xfdzx.android.apps.shop.evenbus.FamilyRefreshMessage;
import cn.xfdzx.android.apps.shop.evenbus.FoundGoodSelectIdMessage;
import cn.xfdzx.android.apps.shop.evenbus.PostCarNumMessage;
import cn.xfdzx.android.apps.shop.fragment.OneFragment;
import cn.xfdzx.android.apps.shop.presenter.ShopCarListPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;

/**
 * @author gzk
 * @date 2020/3/26 0026
 * description：购物车社区零售
 */
public class CarVideoCommunityRetailFragment extends BaseMvpFragment<ShopCarListPresenter> implements ShopCarListContract.View {

    RecyclerView rvCarRetail;

    private List<ShopCarListBeen.DataBean.ValidBean> mList;
    private CarRetailAdapter carRetailAdapter;
    private LinearLayout llCarEmpty;//购物车为空

    @Override
    protected void initView(View view) {
        mPresenter = new ShopCarListPresenter();
        mPresenter.attachView(this);
        mList = new ArrayList<>();
        rvCarRetail = view.findViewById(R.id.rv_retail);
        llCarEmpty = view.findViewById(R.id.ll_car_empty);

//        EventBus.getDefault().register(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvCarRetail.setLayoutManager(linearLayoutManager);
        rvCarRetail.setItemAnimator(new DefaultItemAnimator());
        carRetailAdapter = new CarRetailAdapter(getContext(), mList, mPresenter);
        carRetailAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {

            }
        });
        rvCarRetail.setAdapter(carRetailAdapter);

    }

    @Override
    public void onStart() {
        super.onStart();
        if (getArguments().getInt("type") == 0) {
            mPresenter.initData("0");
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_car_community_retail;
    }

    @Override
    public void showLoading() {
        if (TYPE_IS_RETAIL == 0) {
            if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
                ProgressDialog.getInstance().show(getActivity());
            }
        }
    }


    @Override
    public void hideLoading() {
        if (TYPE_IS_RETAIL == 0) {
            ProgressDialog.getInstance().dismiss();
        }
    }

    @Override
    public void onError(Throwable throwable) {

    }

//    @Subscribe(threadMode = ThreadMode.POSTING)
//    public void XXX(CarVideoInitDataMessage messageEvent) {
//        if (messageEvent.getMessage() == 0) {
//
//            mPresenter.initData("0");
//        }
//    }

    @Override
    public void onInitSuccess(ShopCarListBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {

            mList.clear();
            mList.addAll(bean.getData().getValid());
            for (ShopCarListBeen.DataBean.ValidBean validBean : bean.getData().getInvalid()) {
                validBean.setIs_select(true);
                for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX goodsBeanX : validBean.getGoods()) {
                    goodsBeanX.setIs_select(true);
                }
            }
            if (bean.getData().getInvalid().size() != 0) {
                for (int i = 1; i < bean.getData().getInvalid().size(); i++) {
                    bean.getData().getInvalid().get(0).getGoods().addAll(bean.getData().getInvalid().get(i).getGoods());
                }

                bean.getData().getInvalid().get(0).setIs_shixiao(true);
                mList.add(bean.getData().getInvalid().get(0));
            }
            carRetailAdapter.notifyDataSetChanged();
            if (mList.size() == 0) {
                llCarEmpty.setVisibility(View.VISIBLE);
            } else {
                llCarEmpty.setVisibility(View.GONE);
            }
        } else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onUpdateCarNumSuccess(BaseObjectBean bean, int number, int one_position, int position) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            EventBus.getDefault().post(new FamilyRefreshMessage(mList.get(one_position).getGoods().get(position).getProductInfo().getId()+"",number));
            mList.get(one_position).getGoods().get(position).setCart_num(number);
            carRetailAdapter.notifyDataSetChanged();

        } else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRemoveGoodSuccess(BaseObjectBean bean, int one_position, int position, boolean isAll) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            //刷新购物车数据
            EventBus.getDefault().post(new PostCarNumMessage());
            if (mList.get(one_position).getGoods().size() == 1 || isAll) {
                for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX  goodsBeanX:mList.get(one_position).getGoods()) {
                    EventBus.getDefault().post(new FamilyRefreshMessage(goodsBeanX.getProductInfo().getId() + "", 0));
                }
                mList.remove(one_position);
                if (mList.size() == 0) {
                    llCarEmpty.setVisibility(View.VISIBLE);
                } else {
                    llCarEmpty.setVisibility(View.GONE);
                }
            } else {
                EventBus.getDefault().post(new FamilyRefreshMessage(mList.get(one_position).getGoods().get(0).getId() + "", 0));
                mList.get(one_position).getGoods().remove(position);
            }
            carRetailAdapter.notifyDataSetChanged();
        } else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onConfirmSuccess(ConfirmGoodBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            Intent intent = new Intent(getActivity(), ConfirmOrderActivity.class);
            intent.putExtra("confirm", bean.getData());
            startActivity(intent);
        } else if (bean.getStatus() == 2000) {
            Toast.makeText(getActivity(), "此商铺已打烊", Toast.LENGTH_SHORT).show();
        } else {

            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void dialog_num(int one_position, int two_position) {
        //这里无用
    }

    public static CarVideoCommunityRetailFragment newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
//        bundle.putLong(Constants.UID, userUid);
        CarVideoCommunityRetailFragment fragment = new CarVideoCommunityRetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroyView() {
//        if (EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().unregister(this);
//        }
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {

        mList.clear();
        mList = null;
        super.onDestroy();
    }
}
